import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jinke/page/statistics/models/ProjectStatistical.dart';

import '../../utils/colors_util.dart';
import '../../utils/util.dart';
import 'index.dart';

class StatisticsPage extends StatelessWidget {
  StatisticsPage({Key? key}) : super(key: key);
  final controller = Get.put(StatisticsController());
  // 主视图
  Widget _buildView() {
    ProjectStatistical projectStatistical = controller.state.projectStatistical;
    return Column(
      children: [
        Container(
          margin: EdgeInsets.all(10.r),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Container(
                width: 58.r,
                height: 58.r,
                margin: EdgeInsets.all(5.r),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(2.r)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(projectStatistical.phaseStatus![0].cou.toString(),style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                    Text('项目总量',style: TextStyle(color: Color(0xff666666),fontSize: 12.r,fontWeight: FontWeight.w500),),
                  ],
                ),
              ),
              Container(
                width: 58.r,
                height: 58.r,
                margin: EdgeInsets.all(5.r),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(2.r)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(projectStatistical.phaseStatus![1].cou.toString(),style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                    Text('已完成',style: TextStyle(color: Color(0xff666666),fontSize: 12.r,fontWeight: FontWeight.w500),),
                  ],
                ),
              ),
              Container(
                width: 58.r,
                height: 58.r,
                margin: EdgeInsets.all(5.r),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(2.r)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(projectStatistical.phaseStatus![2].cou.toString(),style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                    Text('进行中',style: TextStyle(color: Color(0xff666666),fontSize: 12.r,fontWeight: FontWeight.w500),),
                  ],
                ),
              ),
              Container(
                width: 58.r,
                height: 58.r,
                margin: EdgeInsets.all(5.r),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(2.r)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(projectStatistical.phaseStatus![3].cou.toString(),style: TextStyle(color: ColorsUtil.orange,fontSize: 15.r,fontWeight: FontWeight.bold),),
                    Text('临期',style: TextStyle(color: Color(0xff666666),fontSize: 12.r,fontWeight: FontWeight.w500),),
                  ],
                ),
              ),
              Container(
                width: 58.r,
                height: 58.r,
                margin: EdgeInsets.all(5.r),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(2.r)
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(projectStatistical.phaseStatus![4].cou.toString(),style: TextStyle(color: ColorsUtil.red,fontSize: 15.r,fontWeight: FontWeight.bold),),
                    Text('逾期',style: TextStyle(color: Color(0xff666666),fontSize: 12.r,fontWeight: FontWeight.w500),),
                  ],
                ),
              )

            ],
          ),
        ),


        Expanded(child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.all(15.r),
                width: 1.sw,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.r),
                    color: Colors.white
                ),
                padding: EdgeInsets.all(10.r),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,

                  children: [
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text('项目阶段统计',style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                    ),

                    SizedBox(height: 15.r,),
                    BrnDoughnutChart(
                      width: 150,
                      height: 150,
                      ringWidth: 75,
                      data: controller.state.doughnutData??[],
                      // selectedItem: controller.state.selectedItem,
                      // showTitleWhenSelected: true,
                      selectCallback: (BrnDoughnutDataItem? selectedItem) {

                        controller.state.selectedItem = selectedItem;
                        controller.update();

                      },
                    ),

                    SizedBox(height: 15.r,),
                    GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        mainAxisSpacing: 10.r,
                        crossAxisSpacing: 10.r,
                        childAspectRatio: 5
                    ), itemBuilder: (context,index){
                      return Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: 8.r,
                              height: 8.r,
                              margin: EdgeInsets.only(right: 5.r),
                              decoration: BoxDecoration(
                                  color: controller.colors[index],
                                  borderRadius: BorderRadius.circular(180)
                              ),
                            ),

                            Text(
                                controller.state.projectStatistical.phaseCode![index].code??'',
                                style: TextStyle(fontSize: 14.r)
                            ),
                            SizedBox(width: 10.r,),
                            Text(controller.state.projectStatistical.phaseCode![index].cou.toString(),style: TextStyle(fontSize: 14.r),),
                            Text('\t\t |\t\t ${(controller.state.projectStatistical.phaseCode![index].cou!/controller.count1 *100).toStringAsFixed(2)}%'),

                          ],
                        ),
                      );
                    },itemCount: controller.state.projectStatistical.phaseCode!.length,physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,)
                  ],
                ),
              ),
              SizedBox(height: 20.r,),
              Container(
                width: 333.w,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.r),
                    color: Colors.white
                ),
                padding: EdgeInsets.all(10.r),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,

                  children: [
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text('项目类型统计',style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                    ),

                    SizedBox(height: 15.r,),
                    BrnDoughnutChart(
                      width: 150,
                      height: 150,
                      ringWidth: 35,
                      data: controller.state.doughnutDataByType??[],
                      // selectedItem: controller.state.selectedItemByType,
                      // showTitleWhenSelected: true,
                      selectCallback: (BrnDoughnutDataItem? selectedItem) {
                        controller.state.selectedItemByType = selectedItem;
                        controller.update();
                      },
                    ),
                    SizedBox(height: 15.r,),
                    ListView.builder(itemBuilder: (context,index){
                      return Container(
                        width: 100.r,
                        height: 30.r,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: 30.r,
                              height: 8.r,
                              margin: EdgeInsets.only(right: 15.r),
                              decoration: BoxDecoration(
                                  color: controller.colors[index],
                              ),
                            ),
                            SizedBox(child: Text(
                                controller.state.projectStatistical.projectType![index].code??'',
                                style: TextStyle(fontSize: 14.r)
                            ),width: 60.r,),
                            SizedBox(width: 10.r,),
                            Text(controller.state.projectStatistical.projectType![index].cou.toString(),style: TextStyle(fontSize: 14.r),),
                            Text('\t\t\t${(controller.state.projectStatistical.projectType![index].cou!/controller.count2 *100).toStringAsFixed(2)}%'),
                          ],
                        ),
                      );
                    },itemCount: controller.state.projectStatistical.projectType!.length,physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,)
                  ],
                ),
              ),
            Container(
              width: 333.w,
              margin: EdgeInsets.only(top: 20.r),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4.r),
                  color: Colors.white
              ),
              padding: EdgeInsets.all(10.r),
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text('各市/区项目进展统计',style: TextStyle(color: Color(0xff333333),fontSize: 15.r,fontWeight: FontWeight.bold),),
                  ),
                  SizedBox(height: 10.r,),
                  ListView.builder(itemBuilder: (context,index){
                    if(index==0){
                      return Container(
                        height: 45.r,
                        color: ColorsUtil.textBg,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(child: Center(child: Text('市/区',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text('项目\n总量',style: TextStyle(fontSize: 12.r),))),
                            Expanded(child: Center(child: Text('进行中',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text('临期',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text('逾期',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text('完成量',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text('完成率',style: TextStyle(fontSize: 12.r),),)),

                          ],
                        ),
                      );
                    }else{
                      return SizedBox(
                        height: 40.r,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].orgName??'',style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectSum.toString(),style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectOngoing.toString(),style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectPeriod.toString(),style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectWithinLimit.toString(),style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectPeriod.toString(),style: TextStyle(fontSize: 12.r),),)),
                            Expanded(child: Center(child: Text(controller.state.listCentralEntity![index-1].projectCompletion.toString()+'%',style: TextStyle(fontSize: 12.r),),)),


                          ],
                        ),
                      );
                    }
                    
                  },itemCount: controller.state.listCentralEntity!.length+1,physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,),
                ],
              ),
            ),
              SizedBox(height: 20.r,)

            ],
          ),
        ))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<StatisticsController>(
      builder: (_) {
        return Scaffold(
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
